package com.lee.mysql.controller;


import com.github.pagehelper.PageInfo;
import com.lee.mysql.pojo.User;
import com.lee.mysql.service.UserService;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;

/**
 * 用户表
 */

@RestController
@RequestMapping("/user")
@Api(tags = {"用户表"})
public class UserController {
    @Autowired
    private UserService userService;


    // 登录页面
    @RequestMapping(value = "/getCurUserInfo", method = RequestMethod.GET)
    public Result<User> getCurUserInfo() {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        user = userService.queryOneById(user.getId());

        return new Result<>(Result.SUCCESS,"操作成功",user);
    }


    /** 新增用户 */
    @RequiresPermissions("system:user:add")
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    @ApiOperation(value = "新增用户")
    public Result<String> addRecord(User record){
        if (StringUtils.isBlank(record.getUserName())
                || StringUtils.isBlank(record.getLoginName())
                || StringUtils.isBlank(record.getPassword())){
            return new Result<>(Result.FAIL,"姓名/账号/密码不能为空",null);
        }

        int i = userService.addRecord(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"添加成功",null);
        }else {
            return new Result<>(Result.FAIL,"添加失败",null);
        }
    }

    /** 根据ID修改记录 */
    @RequiresPermissions("system:user:update")
    @RequestMapping(value = "/updateOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID修改记录")
    public Result<String> updateOneById(User record){

        int i = userService.updateOneById(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 修改密码 */
    @RequestMapping(value = "/updatePwdById", method = RequestMethod.POST)
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query"),
            @ApiImplicitParam(name = "oldPwd", value = "旧密码", paramType = "query"),
            @ApiImplicitParam(name = "newPwd", value = "新密码", paramType = "query"),
            @ApiImplicitParam(name = "checkPwd", value = "确认密码", paramType = "query")
    })
    public Result<String> updatePwdById(String userId,String oldPwd,String newPwd,String checkPwd){
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(oldPwd) ||
                StringUtils.isBlank(newPwd) || StringUtils.isBlank(checkPwd)){
            return new Result<>(Result.FAIL,"原密码/新密码/确认密码不能为空",null);
        }

        if (!newPwd.trim().equals(checkPwd.trim())){
            return new Result<>(Result.FAIL,"新密码和确认密码不一致",null);
        }

        int i = userService.updatePwdById(userId,oldPwd,newPwd);

        if (i == 999){
            return new Result<>(Result.FAIL,"原密码不正确",null);
        }else if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 根据ID批量删除记录 */
    @RequiresPermissions("system:user:delete")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID批量删除记录")
    @ApiImplicitParam(name = "ids", value = "id(单个,多个逗号隔开)", paramType = "query")
    public Result<String> deleteByIds(String ids){
        if (StringUtils.isBlank(ids)){
            return new Result<>(Result.FAIL,"请选择要删除的记录",null);
        }

        String[] split = ids.split(",");

        int k = userService.checkIsAdmin(Arrays.asList(split));
        if (k > 0){
            return new Result<>(Result.FAIL,"删除包含系统用户,不能删除!",null);
        }

        int i = userService.deleteByIds(Arrays.asList(split));

        if (i > 0){
            return new Result<>(Result.SUCCESS,"删除成功",null);
        }else {
            return new Result<>(Result.FAIL,"删除失败",null);
        }
    }

    /** 根据ID查询记录 */
    @RequestMapping(value = "/queryOneById", method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查询记录")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    public Result<User> queryOneById(String id){

        User record = userService.queryOneById(id);

        return new Result<>(Result.SUCCESS,"操作成功",record);
    }


    /** 查询分页数据 */
    @RequiresPermissions("system:user:view")
    @RequestMapping(value = "/queryPaging", method = RequestMethod.POST)
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query")
    })
    public Result<PageInfo<User>> queryPaging(User record, Integer page, Integer size) {
        if (page == null) page = 1;
        if (size == null) size = 10;

        Result<PageInfo<User>> result = new Result<PageInfo<User>>();
        PageInfo<User> list = userService.queryPaging(record, page, size);

        if (list == null) list = new PageInfo<>();

        result.setData(list);
        result.setErrorCode(Result.SUCCESS);
        return result;
    }





}
